class MayBe {
  static of(value) {
    return new MayBe(value);
  }

  constructor(value) {
    this._value = value;
  }

  map(fn) {
    return this.isNothing() ? MayBe.of(null) : MayBe.of(fn(this._value));
  }

  isNothing() {
    return this._value === null || this._value === undefined;
  }
}

const r = MayBe.of("hello word").map((v) => v.toUpperCase());
console.log(r);

// MayBe函子不知道那次结果会出错
const r1 = MayBe.of("hello word")
  .map((v) => v.toUpperCase())
  .map((v) => null)
  .map((v) => v + 1);
console.log(r1);
